Divide a given number by 2^k and display the output
The dividing function also works similar to the multiply function. Only the operator changes. So the steps for dividing a number by 2^k are listed below:
- Assign a value, say 5 to a variable ‘k’
- Then, we have to store the value of 2^5 in another variable j.
- Divide the contents of both k and j.
- Display the output .(Here 160 will be the resultant output)
The Python 3 code for basic division is as follows:
Python3
k = 5 j = 2 * * k p = k / / j print (p) |
Output:
0
We can use ‘//’ for integer division and ‘/’ for floor division as per our requirement.And the optimized code using right shift operation will look like :
Python3
k = 5 print (k>>k) |
Output:
0
Both the codes will result in the output ‘0’(integer division).
Note: For more information, refer to Python Bitwise Operators.
All these operations already existed since history. But we forgot to practice using them. For our changing future, let’s adopt these small changes and become a part of it.
Change your way to put logic in your code – Python
Aren’t you bored coding in the same traditional logic over these years. It’s time to bring some differences. Well, be it any programming language. When we are asked to do some sort of basic programming exercises like sorting, searching, prime number checking, etc. we are all driven towards the same boat, which is the same coding logic. Now, we are in the past 2020, so let’s also take an initiative to change our thinking styles when it comes to coding. It is because, only if we think differently, we can code differently.
Lets dive deep into those wonderful coding tips.